package com.dwin.dingnuo.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.TextView;

import com.dwin.dingnuo.R;
import com.dwin.dingnuo.utils.SDCardUtils;
import com.dwin.dingnuo.utils.ToastUtil;


/**
 * 头像设置对话框 有拍照、相册选择、取消选项
 */
public class ChooseDialog implements OnClickListener {
    // SD卡不存在
//    public static final String SDCARD_NOT_EXISTS = "SD卡不存在，无法设置头像";
    private Dialog mDialog = null;
    private Activity mActivity = null;
    // 设置头像
    public static final int HEAD_FROM_ALBUM = 2106; // 图片来自相册
    public static final int HEAD_FROM_CAMERA = 2107; // 图片来自相机

    ClickBack clickBack;

    public ChooseDialog(Activity act) {
        mActivity = act;
    }

    /**
     * 显示设置头像选择对话框
     */
    public void popSelectDialog() {
        if (SDCardUtils.isExistSDCard()) {
            setDialog();
            mDialog.show();
        } else {
            showToast(mActivity.getResources().getString(R.string.sdcard_not_exist));
            return;
        }
    }

    /**
     * 设置dialog窗口样式 布局 点击事件
     */
    private void setDialog() {
        // 此处直接new一个Dialog对象出来，在实例化的时候传入主题
        if (mDialog == null) {
            mDialog = new Dialog(mActivity, R.style.DialogTheme);
            mDialog.setContentView(R.layout.head_set_choice);
            mDialog.setCanceledOnTouchOutside(true);
            Window window = mDialog.getWindow();
            window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.dialog_style);
            window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            TextView takePic = (TextView) mDialog.findViewById(R.id.take_pictures);
            TextView cancelTxt = (TextView) mDialog.findViewById(R.id.select_cancel);

            TextView selectAlbum = (TextView) mDialog.findViewById(R.id.select_photo);
            takePic.setOnClickListener(this);
            selectAlbum.setOnClickListener(this);
            cancelTxt.setOnClickListener(this);
        }
    }

    /**
     * 显示提示信息
     *
     * @param msg 信息
     */
    public void showToast(String msg) {
        ToastUtil.toastShort((Context) mActivity, msg);
    }

    /**
     * 处理各控件点击事件
     */
    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.take_pictures) {
            if (clickBack != null) {
                clickBack.clickInCamera();
            } else {
                startCamera();
            }
            mDialog.dismiss();
        } else if (viewId == R.id.select_photo) {
            if (clickBack != null) {
                clickBack.clickInAlbum();
            } else {
                startAlbum();
            }
            mDialog.dismiss();
        } else if (viewId == R.id.select_cancel) {
            mDialog.dismiss();
        }
    }


    public void setCallBack(ClickBack clickBack) {
        this.clickBack = clickBack;
    }

    /**
     * 开始拍照
     */
    public void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        mActivity.startActivityForResult(intent, HEAD_FROM_CAMERA);
    }

    /**
     * 打开相册 选择照片
     */
    public void startAlbum() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        mActivity.startActivityForResult(intent, HEAD_FROM_ALBUM);
    }

    public interface ClickBack {
        public void clickInCamera();

        public void clickInAlbum();
    }
}
